﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN21_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }   
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN21_1S.aspx");
    }
    #endregion
    #region "---------- Methods -----------"    
    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }
    private void BindData(int ID)
    {
        List<ModelPrice> modelPrice = new List<ModelPrice>();
        DataTable dt = new SPR_ModelPrice().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, modelPrice);
                 
            txtTransactionDate.Text = modelPrice[0].TransactionDate.DateToString();
            ddlSourceData.SelectedValue = modelPrice[0].SourceDataID.ToString();   
            txtAccountNo.Text = modelPrice[0].AccountNo;
            txtModelPriceValue.Text = modelPrice[0].ModelPriceValue.ToString();

            hidModelPriceID.Value = modelPrice[0].ModelPriceID.ToString();
            hidUpdateDate.Value = modelPrice[0].UpdatedDate.ToString();          
        }
    }
    private void BindDDL()
    {
        ddlSourceData.SetData(DDL_TYPE.SourceData, "", false, true);
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqAccountNo.SetRequiredField(lblAccountNo, txtAccountNo, btnSAVE);       
        reqSourceData.SetRequiredField(lblSourceData, ddlSourceData, btnSAVE);
    }
    private void SaveData()
    {
        try
        {
            ModelPrice modelPrice = new ModelPrice();

            modelPrice.ModelPriceID = hidModelPriceID.Value.AsInt32();
            modelPrice.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
            modelPrice.SourceDataID = ddlSourceData.SelectedValue.AsIntIsnull();
            modelPrice.AccountNo = txtAccountNo.Text;
            modelPrice.ModelPriceValue = txtModelPriceValue.Text.AsDecimal().Value;
            modelPrice.SetStandardField();           

            int iID = new SPR_ModelPrice().UPDATE(modelPrice);

            if (iID > 0)
            {                
                //this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN21_2E.aspx?&id={0}", hidModelPriceID.Value.AsInt32())));
                this.JS_Alert(Resources.ifrsResource.SaveData);
                this.BindData(modelPrice.ModelPriceID);             
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN21_2E;
    }
    #endregion
}